﻿/* author: em
 *  email: 124087198@qq.com
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace emSoft
{
    public partial class Form1 : Form
    {
        enum Charset
        {
            Windows = 0,
            Unix,
        }
        bool IsUnix = false;

        public Form1 () {
            InitializeComponent();

            InitComb();
            loadConfig();
        }

        void InitComb () {
            string[] name = Enum.GetNames(typeof(Charset));
            for (int i = 0; i < name.Length; i++)
                DestEncComb.Items.Add(name[i]);
        }


        void loadConfig () {
            LocalConfig.Inst.Load();

            SrcPathBox.Text = LocalConfig.Inst.Get("SrcPath");
            TypesBox.Text = LocalConfig.Inst.Get("Types");
            string str = LocalConfig.Inst.Get("Charset");

            if (!string.IsNullOrEmpty(str)) {
                Charset c = (Charset)Enum.Parse(typeof(Charset), str);
                DestEncComb.SelectedIndex = (int)c;
            }
        }
        void saveConfig() {
            LocalConfig.Inst.Set("SrcPath", SrcPathBox.Text);
            LocalConfig.Inst.Set("Types", TypesBox.Text);

            Charset charset = (Charset)Enum.ToObject(typeof(Charset), DestEncComb.SelectedIndex);
            LocalConfig.Inst.Set("Charset", charset);

            LocalConfig.Inst.Save();
        }

        
        void CheckFormat() {
            Charset en = (Charset)Enum.Parse(typeof(Charset), (string)DestEncComb.SelectedItem);
            switch (en) {
                case Charset.Unix:  IsUnix = true;      break;
                default:            IsUnix = false;     break;
            }
        }

        void SelPathBtn_Click (object sender, EventArgs e) {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            if (!string.IsNullOrEmpty(SrcPathBox.Text)) fb.SelectedPath = SrcPathBox.Text;
            if (fb.ShowDialog() == DialogResult.OK)
                SrcPathBox.Text = fb.SelectedPath;
        }

        void DumpBtn_Click (object sender, EventArgs e) {
            CheckFormat();
            ConvertDir(SrcPathBox.Text, TypesBox.Text);
            MessageBox.Show("Convert success!");
            saveConfig();
        }

        void ConvertDir (string path, string filter) {
            if (Directory.Exists(path)) {
                string[] files = Directory.GetFiles(path);
                foreach (string file in files) {
                    if (!TypeInFilter(file, filter)) continue;
                    ConvertFile(file);
                }

                string[] dirs = Directory.GetDirectories(path);
                foreach (string dir in dirs) ConvertDir(dir, filter);
            }
        }
        void ConvertFile(string path)
        {
            string text = File.ReadAllText(path);
            if (IsUnix)
                text = text.Replace("\r\n", "\n");
            else if (text.IndexOf("\r\n") < 0) {
                text = text.Replace("\n", "\r\n");
            }
            File.WriteAllText(path, text);
        }

        bool TypeInFilter(string file, string filter) {
            string[] types = filter.Split(',');
            string ft = GetType(file);
            if (ft == null) return false;

            foreach (string type in types) {
                if (type == "*" || type == ft) return true;
            }
            return false;
        }
        string GetType(string path) {
            int idx = path.LastIndexOf('.');
            if (idx < 0) return null;
            return path.Substring(idx + 1, path.Length - (idx + 1));
        }
    }
}
